package com.example.wacoffee_user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.wacoffee_user.pojo.dto.couponDto;
import com.example.wacoffee_user.pojo.entity.coupon;
import com.example.wacoffee_user.pojo.vo.couponOrderVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface CouponMapper extends BaseMapper<coupon> {

    @Insert("insert into coupon_order(coupon_id,user_id) values(#{couponId},#{userId})")
    void addCouponToUser(couponOrderVo couponOrderVo);

    @Select("select count(id) from coupon_order where coupon_id=#{couponId} and user_id=#{userId}")
    int existUserToCoupon(couponOrderVo couponOrderVo);

    @Select(
            "select c.id,c.name,c.value,c.min_amount,c.start_time,c.end_time,c.description,c.stock,c.status,co.status as used " +
                    "from coupon c left join coupon_order co on c.id=co.coupon_id " +
                    "where c.start_time<=#{now} and c.end_time>=#{now} and c.stock>0 "
    )
    List<couponDto> getAllCoupon(LocalDateTime now, Integer currentId);
}
